
import { _decorator, Component, Node, Sprite, find, EventTouch, Color } from 'cc';
import { CommandType, Message, MessageType } from '../framework/Message';
import { MessageBase } from '../framework/MessageBase';
import { MessageCenter } from '../framework/MessageCenter';
import { GameData } from '../GameData';
import { BattleManager } from '../BattleManager';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = Move
 * DateTime = Sat Feb 05 2022 15:07:22 GMT+0800 (China Standard Time)
 * Author = vvc_zeng
 * FileBasename = Move.ts
 * FileBasenameNoExtension = Move
 * URL = db://assets/script/player/Move.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

@ccclass('Move')
export class Move extends MessageBase {
    static Instance: Move;
    // 总共已拥有的技能 ID 11,12,3,4
    move: number[];
    // 当前激活的技能 ID 11,12
    activeMove: number[];

    moveComp: Sprite[] = [];
    onLoad() {
        super.onLoad()
        Move.Instance = this;
        this.moveComp = <Sprite[]>this.node.getComponentsInChildren("cc.Sprite");
    }
    init() {
        this.messageType = MessageType.TYPE_MOVE;
    }
    initData() {
        this.move = [];
        this.activeMove = [];
    }
    ReveiveMessage(message: Message) {
        super.ReveiveMessage(message);
        if (message.Command == CommandType.COM_UI_CHOICE_MOVE) {
            // 初始化数据
            this.initData()
            // 选择
            console.log("获得英雄初始技能", message.Content);
            this.getMove(message.Content)
        } else if (message.Command == CommandType.COM_MOVE_INIT) {
            this.showMoveInfo()
            this.node.active = true;
        }
    }
    showMoveInfo() {
        this.node.children.forEach((e) => {
            e.active = true;
        });
        (<Sprite>this.node.children[0].getComponent("cc.Sprite")).spriteFrame = GameData.Instance.moveFrames[this.move[0]];
    }
    getMove(mid) {
        if (this.move.length < 4) {
            // 渲染新技能到视图层
            this.moveComp[this.move.length].spriteFrame = GameData.Instance.moveFrames[mid];
            this.moveComp[this.move.length].node.on(Node.EventType.TOUCH_END, this.toggleMove, this);
            this.move.push(mid);
            this.activeMove.push(mid);
        } else {
            console.log("技能已满，不能再获得新技能")
        }
    }
    toggleMove(e: EventTouch) {
        // 非战斗情况下才能切换技能
        if (BattleManager.Instance.mid == -1) {
            let i = (<Node>e.target).getSiblingIndex();
            let a = this.activeMove.indexOf(this.move[i])
            // 切换技能的激活状态
            if (a > -1) {
                // 如果已激活，则改为不激活
                (<Color>this.moveComp[i].color).a = 100;
                this.activeMove.splice(a, 1);
            } else {
                (<Color>this.moveComp[i].color).a = 255;
                this.activeMove.push(this.move[i]);
            }
            // 显示 move[i] 的描述信息
            MessageCenter.SendCustomMessage(MessageType.TYPE_MAP, CommandType.COM_MAP_SHOWTIPS, [(<Node>e.target).parent.name, i])
        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
